﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using OnlineVideos.Data;
using Common.Library;
using System.IO.IsolatedStorage;
using System.IO;
using OnlineVideos.UI;
using Microsoft.Phone.Tasks;
using OnlineVideos.Entities;
using System.Threading.Tasks;

namespace OnlineVideos
{
    public partial class PlayDownloadedVideos : PhoneApplicationPage
    {
        public PlayDownloadedVideos()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(PlayDownloadedVideos_Loaded);
        }

        void PlayDownloadedVideos_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogEvent("PlayDownloadedVideos Page", true);
                string ShowId = "";
                if (NavigationContext.QueryString.TryGetValue("ShowId", out ShowId))
                {
                    Playvideo(Convert.ToInt32(ShowId));
                }
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in PlayDownloadedVideos_Loaded event In Playdownloadedvideo.cs file.", ex);
            }
        }
        private void Playvideo(int ShowId)
        {
            try
            {
                string linktype = LinkType.Songs.ToString();
                List<ShowLinks> ShowLinksByType = Task.Run(async () => await Constants.connection.Table<ShowLinks>().Where(i => i.ShowID == ShowId && i.LinkType == linktype).ToListAsync()).Result;
                foreach (var linkinfo in ShowLinksByType)
                {
                    using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (var isfs = new IsolatedStorageFileStream(linkinfo.LinkUrl, FileMode.Open, isf))
                        {
                            MediaPlayerLauncher M;
                            M = new MediaPlayerLauncher();
                            M.Media = new Uri(linkinfo.LinkUrl, UriKind.Relative);
                            M.Controls = MediaPlaybackControls.All;
                            M.Show();
                           
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in Playvideo event In Playdownloadedvideo.cs file.", ex);
            }
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("PlayDownloadedVideos Page");
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In Playdownloadedvideo.cs file.", ex);
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In Playdownloadedvideo.cs file.", ex);
            }
        }
        private void imgTitle_MouseEnter(object sender, MouseEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
    }
}